package com.xszx.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpSession;

import com.xszx.service.SuperService;
import com.xszx.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import com.xszx.bean.EmpInfo;
import com.xszx.bean.LoginUser;
import com.xszx.bean.UserAttendance;
import com.xszx.bean.UserVocate;
import com.xszx.bean.WorkTime;

@Controller
@CrossOrigin
public class UserController {

    @Autowired
    UserService userService;
    @Autowired
    SuperService superService;


    @RequestMapping("/toUserBody")
    public String toUserBody(HttpSession session, Model model) {
        LoginUser loginUser = (LoginUser) session.getAttribute("loginUser");
        String empNo = loginUser.getAccount();
        EmpInfo empInfo = userService.selectIndividualInfo(empNo);
        SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日");
        String birthday = format.format(empInfo.getBirthday());
        model.addAttribute("birthday", birthday);
        model.addAttribute("empInfo", empInfo);
        return "userBody.html";
    }

    @RequestMapping("/daka")
    public String daka() {
        return "daka.html";
    }

    @RequestMapping("/qingjia")
    public String qingjia() {
        return "qingjia.html";
    }

    @RequestMapping("/toAttdView")
    public String toAttdView() {
        return "attdView.html";
    }

    @RequestMapping("/toVacateView")
    public String toVacateView() {
        return "vacateView.html";
    }

    @RequestMapping("/addAttendance")
    @ResponseBody
    public boolean addAttendance(@RequestBody UserAttendance userAttendance) {
        System.out.println("打卡信息：" + userAttendance.toString());
        System.out.println("打卡时间信息:" + userAttendance.getTime().getTime());
        WorkTime workTime = superService.selectWorkTime();
        @SuppressWarnings("deprecation")
        int hour = userAttendance.getTime().getHours();
        int minute = userAttendance.getTime().getMinutes();
        int year = userAttendance.getTime().getYear() + 1900;
        int month = userAttendance.getTime().getMonth() + 1;
        String yearAndMonth = "" + year + "-" + month;
        System.out.println("日期信息：" + year + "," + month);
        System.out.println(yearAndMonth);
        int workStart = workTime.getStart().getHours();
        int workEnd = workTime.getEnd().getHours();
        System.out.println("打卡时间：" + hour + "," + minute);
//		System.out.println("打卡调整时间："+(hour+8));
        System.out.println("工作时间" + workStart + "," + workEnd);
//		if(userAttendance.getType().equals("上班")&&userAttendance.getTime().getTime()>workTime.getStart().getTime()){
//			userAttendance.setStatus("迟到");
//		}else if(userAttendance.getType().equals("下班")&&userAttendance.getTime().getTime()<workTime.getEnd().getTime()){
//			userAttendance.setStatus("早退");
//		}else {
//			userAttendance.setStatus("正常");
//			System.out.println("进来了吗？");
//		}
        if (userAttendance.getType().equals("上班") && hour > workStart) {
            userAttendance.setStatus("迟到");
        } else if (userAttendance.getType().equals("下班") && hour < workEnd) {
            userAttendance.setStatus("早退");
        } else {
            userAttendance.setStatus("正常");
        }
        Date date = userAttendance.getTime();
        System.out.println(userAttendance.getTime());
        userAttendance.setYearAndMonth(yearAndMonth);
        System.out.println("要存入数据库的打卡信息：" + userAttendance.toString());
        int i = userService.addAttendance(userAttendance);
        if (i > 0) {
            return true;
        } else {
            return false;
        }
    }

    @RequestMapping("/addVocate")
    @ResponseBody
    public boolean addVocate(@RequestBody UserVocate userVocate) {
        userVocate.setStatus(0);//0为未审批，1为已审批
        int i = userService.addVocate(userVocate);
        if (i > 0) {
            return true;
        } else {
            return false;
        }
    }

    @RequestMapping("/getMyVacate")
    @ResponseBody
    public Map<String, Object> getMyVacate(HttpSession session, @RequestParam int limit, @RequestParam int page) {
        LoginUser loginUser = (LoginUser) session.getAttribute("loginUser");
        String empNo = loginUser.getAccount();
        int offset = (page - 1) * limit;
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("limit", limit);
        map.put("offset", offset);
        map.put("empNo", empNo);
        List<UserVocate> list = userService.selectMyVacate(map);
        int count = userService.selectMyVacateCount(empNo);
        map.put("count", count);
        map.put("data", list);
        map.put("code", "0");
        map.put("msg", "");
        return map;
    }

    @RequestMapping("/getMyAttd")
    @ResponseBody
    public Map<String, Object> getMyAttd(HttpSession session, @RequestParam int limit, @RequestParam int page) {
        LoginUser loginUser = (LoginUser) session.getAttribute("loginUser");
        String empNo = loginUser.getAccount();
        int offset = (page - 1) * limit;
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("limit", limit);
        map.put("offset", offset);
        map.put("empNo", empNo);
        List<UserAttendance> list = userService.selectMyAttd(map);
        int count = userService.selectMyAttdCount(empNo);
        map.put("count", count);
        map.put("data", list);
        map.put("code", "0");
        map.put("msg", "");
        return map;
    }
}
